let apiUrl = '';
let imgUrl = '';
let api = {}

/**
 * API GET 请求
 */
var apiGet = function (apiText, url, params, form) {
	return apiRequest(apiText, "GET", url, params, form);
}

/**
 * * API POST 请求
 * form：是否使用'application/x-www-form-urlencoded'的数据格式
 */
var apiPost = function (apiText, url, params, form) {
	return apiRequest(apiText, "POST", url, params, form);
}

/**
 * API请求
 */
function apiRequest(apiText, method, url, params, form) {
	return new Promise((resolve, reject) => {
		let type = "text/html";
		if(method == "POST") type = "application/json";
		if(form) type = "application/x-www-form-urlencoded";
		uni.request({
			url: url,
			method: method,
			data: params,
			header: {
				'Content-Type': type,
				'X-Access-Token': uni.getStorageSync('token') || ''
			},
			//请求成功
			success: function (res) {
				log(apiText + '-' + url, res.data);
				resolve(res.data);
				
				// if(res.data.success){
				// 	log(apiText + '-' + url, res.data);
				// 	resolve(res.data);
				// }else{
				// 	//全局拦截-失败提示
				// 	log('错误 - ' + apiText + '-' + url, res.data);
				// 	reject(res);
				// 	if(res.data.message) uni.showToast({title: res.data.message, icon: 'none', duration: 3500});
				// 	if(res.data.status == 500){
				// 		// 全局拦截token失效
				// 		uni.removeStorageSync('token');
				// 		uni.reLaunch({url:'/pages/public/login', complete: res => {
				// 			console.log(res);
				// 		}});
				// 	}
				// }
			},
			//请求失败
			fail: function (err) { 
				console.log('请求失败', err);
				reject(err); 
			},
			//请求完成后执行的函数
			complete: function () { 
			}
		});
	})
}

/**
 * 非省略号打印
 */
function log(name, value) {
	console.log(name, value ? JSON.parse(JSON.stringify(value)) : '');
}


/**
 * 导出公开的API函数
 */
module.exports = {
	setUrl(_apiUrl, _imgUrl){
		console.log(_apiUrl, _imgUrl)
		apiUrl = _apiUrl;
		imgUrl = _imgUrl;
	},
	
	getImgUrl(){
		return imgUrl;
	},
	
	getSessionkey(code){
		return apiGet('获取openid和sessionkey', apiUrl + '/appletApi/signing/getSessionkey',{code: code});
	},
	
	getPhone(params){
		return apiGet('解密手机号', apiUrl + '/appletApi/signing/getPhoneByKey',params);
	},
	
	queryServiceTaskById(params){
		return apiGet('查询签约任务信息', apiUrl + '/appletApi/signing/queryServiceTaskById',params);
	},
	
	addMember(params){
		return apiPost('添加签约人才', apiUrl + '/appletApi/signing/addMember',params);
	},
	
	editMember(params){
		return apiPost('编辑签约人才（签约）', apiUrl + '/appletApi/signing/editMember',params);
	},
	
	queryPrivacyAgreement(params){
		return apiGet('查询用户协议和隐私政策', apiUrl + '/appletApi/signing/queryPrivacyAgreement',params);
	},
	
	/**
	 * 图片上传
	 */
	uploadImg(params, callBack){
		return uni.uploadFile({
			url: apiUrl + '/appletApi/signing/uploadIdCard', 
			filePath: params.file,
			name: 'file',
			header: {
				'X-Access-Token': uni.getStorageSync('token')
			},
			formData: params.formData,
			success(uploadFileResult){
				const uploadFileRes = JSON.parse(uploadFileResult.data) || {};
				console.log('上传图片：', uploadFileRes);
				if(callBack) callBack(uploadFileRes);
			}, 
			fail(){
				if(callBack) callBack({
					status: false,
					res: '网络链接错误'
				});
			}
		});
	},
	
};